package pl.sind.http.auth;

import java.io.UnsupportedEncodingException;

import pl.sind.blip.Encoder;
import pl.sind.http.HttpConnector;
import pl.sind.http.HttpHeader;
import pl.sind.http.HttpRequest;

public class BasicAuthenticationStrategy implements AuthenticationStrategy{
	
	private String authentication;
	private HttpHeader[] authHeaders;
	
	public BasicAuthenticationStrategy(String username, String password) {
		String auth = username + ":" + password;
		try {
			auth = new String(Encoder.encode(auth.getBytes("UTF-8")), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// Java w/o UTF-8?
		}
		this.authentication = auth;
		authHeaders = new HttpHeader[] { new HttpHeader("Authorization",
				"Basic " + authentication) };
	}

	public void perform(HttpRequest<?> con) {
		con.addRequestHeaders(authHeaders);
	}

	public void initialize(HttpConnector<?> con) {}

}
